//
//  SAMemberSendGroupVC.m
//  saasRetail
//
//  Created by mac on 2023/6/4.
//

#import "SAMemberSendGroupVC.h"
#import "SAMemberSendGroupCell.h"
#import "SAMemberCelebrateVC.h"
#import "SAMemberSatisfyVC.h"
#import "SASmsDiySendListAPI.h"
#import "SASmsAutoSendListModel.h"

@interface SAMemberSendGroupVC ()<UITableViewDelegate, UITableViewDataSource, HTTPAPIDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SASmsDiySendListAPI *smsDiySendListAPI;
@property (nonatomic, strong) NSMutableArray<SASmsAutoSendListModel *> *dataMarr;

@end

@implementation SAMemberSendGroupVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.smsDiySendListAPI == api) {
        self.dataMarr = [SASmsAutoSendListModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
        [self.tableView reloadData];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
   
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAMemberSendGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAMemberSendGroupCell" forIndexPath:indexPath];
    cell.model = self.dataMarr[indexPath.section];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
    if (indexPath.section == 0) {
        SAMemberCelebrateVC *vc = [SAMemberCelebrateVC new];
        vc.tempId = self.dataMarr[indexPath.section].id;
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        SAMemberSatisfyVC *vc = [SAMemberSatisfyVC new];
        vc.tempId = self.dataMarr[indexPath.section].id;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(140);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScaleNum(0.01);
}


#pragma mark - initUI
-(void)initUI {
    
    [self setNavigationItemBackBBIAndTitle:@"自定义发群"];
    [self setBgImage:nil];
    self.view.backgroundColor = kBgColor;
    
    [self.view addSubview:self.tableView];
    
    [self.smsDiySendListAPI sendRequestToServerWithActivityViewAndMask:GET];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kBgColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SAMemberSendGroupCell class] forCellReuseIdentifier:@"SAMemberSendGroupCell"];
    }
    return _tableView;
}

-(SASmsDiySendListAPI *)smsDiySendListAPI {
    if (!_smsDiySendListAPI) {
        _smsDiySendListAPI = [SASmsDiySendListAPI apiWithDelegate:self];
    }
    return _smsDiySendListAPI;
}

-(NSMutableArray<SASmsAutoSendListModel *> *)dataMarr {
    if (!_dataMarr) {
        _dataMarr = [NSMutableArray array];
    }
    return _dataMarr;
}

@end
